<?php

namespace App\Services\Account;

use Carbon\Carbon;
use App\Models\User;
use App\Models\VipPurchase;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;


class AccountBrokerages 
{

	public function __construct(User $user)
    {
        $this->user = $user;
    }

    protected function record(BigDecimal $money, $remark, $reason, $data = [])
    {
        DB::transaction(function () use ($money, $remark, $reason, $data) {
        	
            $userMoneyWillBe = big_decimal($this->user->dividend_brokerage)->plus($money);
            
            $arr = [
            	'money' => (string)$money,
                'remark' => $remark,
                'reason' => $reason,
                'is_taken' => false
            ];

            if(!empty($data)){
				$arr['product_id'] = $data['product_id'];
            	$arr['product_sku_id'] = $data['product_sku_id'];
            	$arr['order_id'] = $data['order_id'];
                $arr['seckill_product_id'] = isset($data['seckill_product_id']) ? $data['seckill_product_id'] : 0;
                $arr['pay_money'] = isset($data['pay_money']) ? $data['pay_money'] : 0;


            }
            //$this->user->update(['dividend_brokerage' => (string)$userMoneyWillBe]);
            $this->user->brokerages()->create($arr);

            
        });
    }


    public function orderPaid($money, $data = [])
	{
		$money = big_decimal($money);

		$this->record($money, 'pay_order', '下级用户商品购买返还红包', $data);
	}

     public function userOrderPaid($money, $data = [])
    {
        $money = big_decimal($money);

        $this->record($money, 'pay_order', '用户商品购买返还红包', $data);
    }
	
	/*public function vipPaid($money, $data = [])
	{
		$money = big_decimal($money);
		$this->record($money, 'pay_vip_purchase', '下级VIP购买返佣', $data);

	}*/

    

    public function allBrokerage()
    {
        return $this->user->brokerages()
            ->whereNull('disabled_at')
            ->where(function (Builder $query) {
                $query->where('remark', 'pay_vip_purchase')
                    ->orWhere('remark', 'pay_order');
            });
    }

    public function dayBrokerage()
    {
        return $this->user->brokerages()
            ->whereNull('disabled_at')
            ->whereDate('created_at', Carbon::today())
            ->where(function (Builder $query) {
                $query->where('remark', 'pay_vip_purchase')
                    ->orWhere('remark', 'pay_order');
            });
    }

    public function monthBrokerage()
    {
        return $this->user->brokerages()
            ->whereNull('disabled_at')
            ->whereYear('created_at', Carbon::today()->year)
            ->whereMonth('created_at', Carbon::today()->month)
            ->where(function (Builder $query) {
                $query->where('remark', 'pay_vip_purchase')
                    ->orWhere('remark', 'pay_order');
            });
    }

    public function lastMonthBrokerage()
    {
        $lastMonth = Carbon::today()->subMonth();
        return $this->user->brokerages()
            ->whereNull('disabled_at')
            ->whereYear('created_at', $lastMonth->year)
            ->whereMonth('created_at',  $lastMonth->month)
            ->where(function (Builder $query) {
                $query->where('remark', 'pay_vip_purchase')
                    ->orWhere('remark', 'pay_order');
            });
    }
    
}
